package com.app.signing.view.room.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.app.signing.view.room.data.RoomSeatData;
import java.util.List;

/**
 * @author wangpeifeng
 * @date 2018/05/09 14:42
 *
 * 房间view
 */
public class RoomView extends FrameLayout implements OnSeatClickListener {

    private RecyclerView recyclerView;
    private OnSeatClickListener onSeatClickListener;
    private List<List<RoomSeatData>> roomList;
    private RoomViewAdapter adapter;

    public RoomView(@NonNull Context context) {
        this(context, null);
    }

    public RoomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoomView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        recyclerView = new RecyclerView(context);
        addView(recyclerView, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
    }

    public void setData(List<List<RoomSeatData>> roomList) {
        this.roomList = roomList;
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RoomViewAdapter(roomList, this);
        recyclerView.setAdapter(adapter);
    }

    public void setOnSeatClickListener(OnSeatClickListener onSeatClickListener) {
        this.onSeatClickListener = onSeatClickListener;
    }

    public void updateSeatData(RoomSeatData seatData) {
        int row = seatData.getRowCount();
        int column = seatData.getColumnCount();
        List<RoomSeatData> seatDataList = roomList.get(row);
        seatDataList.set(column, seatData);
        adapter.setRoomList(roomList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(RoomSeatData seatData) {
        if (onSeatClickListener != null) {
            onSeatClickListener.onClick(seatData);
        }
    }
}
